package pokerbot.entities;

import java.util.ArrayList;
import java.util.List;
import pokerbot.Constants;
import pokerbot.exceptions.CardException;

public class Deck {
    
    private final List<Card> actualCards = new ArrayList<>();
    private final List<Card> outOfDateCards = new ArrayList<>();
    private final List<Card> allCards = new ArrayList<>();

    public Deck() {
        allCards.addAll(Constants.getAllCards());
        actualCards.addAll(allCards);
    }
    
    private void emptyCardsCollection(List<Card> cardsCollection) {
        int cardsCollectionSize = cardsCollection.size();
        for (int i = 0; i < cardsCollectionSize; i++) {
            cardsCollection.remove(0);
        }
    }
    
    public void restoreDeck() {
        emptyCardsCollection(actualCards);
        emptyCardsCollection(outOfDateCards);
        actualCards.addAll(allCards);
    }

    public int getActualCardsSize() {
        return actualCards.size();
    }

    public int getOutOfDateCardsSize() {
        return outOfDateCards.size();
    }

    public int getAllCardsSize() {
        return allCards.size();
    }
    
    public void passCard(Card card) throws CardException {
        if (actualCards.isEmpty()) {
            throw new CardException("Deck of actual cards is empty");
        }
        if (!actualCards.contains(card)) {
            throw new CardException("Deck of actual cards does not contain card " + card);
        }
        actualCards.remove(card);
        outOfDateCards.add(card);
    }
    
    /*
    *  Method for testing. To remove
    */
    public Card testGetCardFromDeck() {
        if (actualCards.isEmpty()) {
            throw new UnsupportedOperationException("Deck of actual cards is empty");
        }
        Card card = actualCards.get(0);
        actualCards.remove(card);
        outOfDateCards.add(card);
        return card;
    }

    @Override
    public String toString() {
        return new StringBuilder("actualCards size = ").append(actualCards.size())
                .append("\noutOfDateCards size = ").append(outOfDateCards.size())
                .append("\nallCards size = ").append(allCards.size())
                .append("\nallCards = ").append(allCards)
                .toString();
    }
    
}
